package com.shijie.controller.app;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shijie.component.AppUserService;
import com.shijie.core.Result;
import com.shijie.core.ResultGenerator;
import com.shijie.core.annotation.NoLogin;
import com.shijie.domain.entity.UserAddress;
import com.shijie.domain.entity.Users;
import com.shijie.domain.vo.BaseUserVo;
import com.shijie.service.UserAddressService;
import com.shijie.service.UsersService;
import me.chanjar.weixin.common.exception.WxErrorException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/*
 *  @author: smart boy
 *  @date: 2018-08-06
 */
@RestController
@RequestMapping("/app/address")
public class AppUserAddressController {

    @Resource
    private UserAddressService userAddressService;

    @Resource
    private AppUserService appUserService;

    @GetMapping
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<UserAddress> list = userAddressService.selectAddressByUserId(appUserService.getCurrentUser().getId());
        PageInfo<UserAddress> pageInfo = new PageInfo<>(list);
        return ResultGenerator.success(pageInfo);
    }

    @GetMapping("/@default")
    public Result getDefultAddress() {
        List<UserAddress> list = userAddressService.selectAddressByUserId(appUserService.getCurrentUser().getId());
        if (list != null && list.size() > 0) {
            return ResultGenerator.success(list.get(0));
        } else {
            return ResultGenerator.success();
        }

    }

    @PostMapping
    public Result save(@RequestBody UserAddress userAddress) {
        if (StringUtils.isBlank(userAddress.getRecMobile()) ||
                StringUtils.isBlank(userAddress.getRecName()) ||
                StringUtils.isBlank(userAddress.getAddress())) {
            ResultGenerator.error("客户的收获人姓名，手机号，地址请维护好！");
        }
        userAddress.setUserId(appUserService.getCurrentUser().getId());
        userAddress.setUserName(appUserService.getUserName());
        userAddressService.save(userAddress);
        return ResultGenerator.success();
    }

    @PutMapping
    public Result update(@RequestBody UserAddress userAddress) {
        userAddress.setUserId(appUserService.getCurrentUser().getId());
        userAddress.setUserName(appUserService.getUserName());
        userAddressService.updateById(userAddress);
        return ResultGenerator.success();
    }

    @GetMapping(value = "/{id}")
    public Result detail(@PathVariable Integer id) {
        UserAddress userAddress = userAddressService.selectById(id);
        return ResultGenerator.success(userAddress);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        userAddressService.deleteById(id);
        return ResultGenerator.success();
    }
}

